package com.newlink.building.views

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.RadioGroup
import com.newlink.building.R
import com.newlink.building.activities.call.viewmodels.CallViewModel
import com.newlink.building.utils.Module_Phone_Event
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.linphone.core.AudioDevice
import org.linphone.core.tools.Log

/**
 * @author Jake
 * @date 2022/10/11
 * @description AudioRoute 选择
 */
class Module_Phone_AudioRoutesSelectDialog(context: Context, val viewModel: CallViewModel) :
    Dialog(context, R.style.bg_audio_route_dialog) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_audio_route_popup)

        initAudioRouteContent()
    }

    private fun initAudioRouteContent() {

        val routeRadio = findViewById<RadioGroup>(R.id.route_radio)
        findViewById<RadioButton>(R.id.route_bt).apply {
            visibility = if (viewModel.isBluetoothAvailable()) View.VISIBLE else View.GONE
        }
        findViewById<RadioButton>(R.id.route_headset).apply {
            visibility = if (viewModel.isHeadsetAvailable()) View.VISIBLE else View.GONE
        }
        findViewById<RadioButton>(R.id.route_earpiece).apply {
            visibility = if (viewModel.isHeadsetAvailable()) View.GONE else View.VISIBLE
        }
        val currentAudioDevice = viewModel.getCurrentAudioRoute()
        Log.i("[AudioRoutesSelectDialog] initAudioRouteContent  currentAudioDevice:$currentAudioDevice}")
//        val currentAudioDevice = LinPhoneApp.coreContext.core.currentCall?.outputAudioDevice

//        when (currentAudioDevice?.type) {
//            AudioDevice.Type.Earpiece -> {
//                routeRadio.check(R.id.route_earpiece)
//            }
//            AudioDevice.Type.Speaker -> {
//                routeRadio.check(R.id.route_speaker)
//            }
//            AudioDevice.Type.Bluetooth -> {
//                routeRadio.check(R.id.route_bt)
//            }
//            AudioDevice.Type.Headset -> {
//                routeRadio.check(R.id.route_headset)
//            }
//            else -> {
//                routeRadio.check(R.id.route_speaker)
//            }
//        }

        when (currentAudioDevice) {
            AudioDevice.Type.Earpiece -> {
                routeRadio.check(R.id.route_earpiece)
            }

            AudioDevice.Type.Speaker -> {
                routeRadio.check(R.id.route_speaker)
            }

            AudioDevice.Type.Bluetooth -> {
                routeRadio.check(R.id.route_bt)
            }

            AudioDevice.Type.Headset -> {
                routeRadio.check(R.id.route_headset)
            }

            else -> {
                routeRadio.check(R.id.route_speaker)
            }
        }

        routeRadio.setOnCheckedChangeListener { _, id ->
            when (id) {
                R.id.route_speaker -> {
                    viewModel.switchAudioRoute(AudioDevice.Type.Speaker)
                }

                R.id.route_earpiece -> {
                    viewModel.switchAudioRoute(AudioDevice.Type.Earpiece)
                }

                R.id.route_bt -> {
                    viewModel.switchAudioRoute(AudioDevice.Type.Bluetooth)
                }

                R.id.route_headset -> {
                    viewModel.switchAudioRoute(AudioDevice.Type.Headset)
                }

                else -> {
                    viewModel.switchAudioRoute(AudioDevice.Type.Speaker)
                }
            }
            viewModel.showRouteDialog.value = false
            MainScope().launch {
                delay(500)
                withContext(Dispatchers.Main) {
                    viewModel.audioRouteChanged.value = Module_Phone_Event(true)
                }
            }
            dismiss()
        }
    }
}
